<?php

namespace Home\Logic;

/**
 * 商户逻辑
 */
class ShopLogic extends BaseLogic{

    /**
     * 获取商户经验数据
     * @param $id
     * @return array
     */
    public static function getJingYing($id){

        $kbsh = M('kbsh')->where(['id' => $id])->find();

        $alipayPid = $kbsh['user_id'];
        $today = date('Ymd');
        $yestoday = date('Ymd',strtotime('-1 day'));
        $dingdanList = M('dingdan')->alias('d')
            ->join('__SPLIT__ s on d.id=s.dingdan_id')
            ->where(['d.seller_id' => $alipayPid])
            ->field('danhao,buyer,pay_money,sh_money,pay_time')
            ->select();

//        echo M('dingdan')->getLastSql();exit;

        //今日收款
        ;
        $todayDingdan = M('dingdan')->query("SELECT sum(pay_money) as money FROM jd_dingdan WHERE seller_id=".$alipayPid." and  FROM_UNIXTIME(pay_time, '%Y%m%d')=$today");

        $yestodayDingdan = M('dingdan')->query("SELECT sum(pay_money) as money FROM jd_dingdan WHERE seller_id=".$alipayPid." and  FROM_UNIXTIME(pay_time, '%Y%m%d')=$yestoday");
        $todayTiCheng = M('split')->query("SELECT sum(sh_money) as money FROM jd_split WHERE seller_id=".$alipayPid." and  FROM_UNIXTIME(time, '%Y%m%d')=$today");

        $todayTotal = M('dingdan')->query("SELECT count(*) as count FROM jd_dingdan WHERE seller_id=".$alipayPid." and  FROM_UNIXTIME(pay_time, '%Y%m%d')=$today");
        $todaySuccess = M('dingdan')->query("SELECT count(*) as count FROM jd_dingdan WHERE seller_id=".$alipayPid."  and status=1 and FROM_UNIXTIME(pay_time, '%Y%m%d')=$today");

        $success = $todaySuccess ? $todaySuccess[0]['count'] : 0;
        $total = $todayTotal ? $todayTotal[0]['count'] : 0;

//        if($total == 0){
//            $rate = 0;
//        }else{
//            if($success){
//                $rate = $todaySuccess/$todayTotal*100;
//                $rate = sprintf("%.2f",round($rate,2));
//            }else{
//                $rate = 0;
//            }
//        }

        $data = [
            'jinRiShouKuan' => $todayDingdan ? $todayDingdan[0]['money'] : 0,
            'jinRiTiCheng' => $todayTiCheng[0]['money'] ? $todayTiCheng[0]['money'] : 0,
            'ZuoRiShouKuan' => $yestodayDingdan[0]['money'] ? $yestodayDingdan[0]['money'] : 0,
            'jinRiChengGong' => $success,
            'jinRiDingDan' => $total,
//            'jinRiChengGongLv' => $rate,
            'jinRiChengGongLv' => 0,
            'dingDanList' => $dingdanList ? $dingdanList : []
        ];
        return $data;
    }


    public static function delRedisShop($pay_type,$value){
        $redis = new \Predis\Client(C('REDIS'),['prefix' => C('REDIS_PREFIX')]);
        $rediskey = \Home\Logic\PayLogic::getRedisKeyByType($pay_type);
        $redis->lrem($rediskey,1,$value);
    }

    public static function addRedisShop($pay_type,$value){
        $redis = new \Predis\Client(C('REDIS'),['prefix' => C('REDIS_PREFIX')]);
        $rediskey = \Home\Logic\PayLogic::getRedisKeyByType($pay_type);
        $redis->lpush($rediskey,$value);
    }

}